﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Barbar.UrlGuard.Dal.Persist;
using Barbar.UrlGuard.Dal;
using Barbar.UrlGuard.Controls;
using Barbar.UrlGuard.BL;
using System.Globalization;

namespace Barbar.UrlGuard {
  public partial class FormHistory : LocalizableForm, ILocalizable {
    private Guid m_EntryID;
    private string m_Url;
    private string m_DateColumnName;
    private string m_StatusColumnName;
    private string m_LengthColumnName;

    private enum GridColumns : int {
      ColumnDateChecked = 0,
      ColumnStatus = 1,
      ColumnDuration = 2
    }

    public FormHistory(Guid entryID, string url) {
      m_EntryID = entryID;
      m_Url = url;
      InitializeComponent();
    }

    protected override void OnLoad(EventArgs e) {
      base.OnLoad(e);
      if (!this.DesignMode) {
        LoadData();
      }
    }

    private DataTable CreateDataTable() {
      DataTable result = new DataTable();
      DataColumn colDateChecked = new DataColumn(m_DateColumnName, typeof(DateTime));
      DataColumn colStatus = new DataColumn(m_StatusColumnName, typeof(int));
      DataColumn colLength = new DataColumn(m_LengthColumnName, typeof(double));

      result.Columns.AddRange(new DataColumn[] { colDateChecked, colStatus, colLength });
      return result;
    }

    private void LoadData() {
      IHistoryPersistor persistor = ServiceLocator.Instance.Resolve<IHistoryPersistor>();
      DataTable stub = CreateDataTable();
      foreach (MonitoringCheck check in persistor.GetAllChecks(m_EntryID)) {
        DataRow row = stub.NewRow();
        row.ItemArray = new object[] { check.CheckDate, check.StatusCode, check.RequestLength };
        stub.Rows.InsertAt(row, 0);
      }
      grid.DataSource = stub;
      grid.Columns[(int)GridColumns.ColumnDateChecked].Width = (int)(grid.Width * 0.3);
      grid.Columns[(int)GridColumns.ColumnStatus].Width = (int)(grid.Width * 0.3);
      grid.Columns[(int)GridColumns.ColumnStatus].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
      grid.Columns[(int)GridColumns.ColumnDuration].Width = (int)(grid.Width * 0.4);
      grid.Columns[(int)GridColumns.ColumnDuration].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
      grid.Columns[(int)GridColumns.ColumnDuration].DefaultCellStyle.Format = "F2";
    }

    #region ILocalizable Members

    public void Localize(ILocalizator localizator) {
      if (localizator == null)
        throw new ArgumentNullException("localizator");

      string format = localizator.Translate("FormHistory_Title");
      Text = string.Format(CultureInfo.InvariantCulture, format, m_Url);

      m_DateColumnName = localizator.Translate("FormHistory_Date");
      m_LengthColumnName = localizator.Translate("FormHistory_Length");
      m_StatusColumnName = localizator.Translate("FormHistory_Status");
    }

    #endregion
  }
}
